package cn.edu.ctbu.yrx.service;

import cn.edu.ctbu.yrx.dao.CourseScoreRepository;
import cn.edu.ctbu.yrx.entity.CourseScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class CourseScoreService {

    @Autowired
    private CourseScoreRepository courseScoreRepository  ;

    public Page<CourseScore> findByStudentStuId(Integer studentId, Pageable pageable) {
        return courseScoreRepository.findByStudentStuId(studentId, pageable);
    }

    public Page<CourseScore> findByTeacherTeaId(Integer teacherId, Pageable pageable) {
        return courseScoreRepository.findByTeacherTeacherId(teacherId, pageable);
    }

    public CourseScore updateScore(Integer id, String score) {
        // 1. 根据ID查找学生课程记录
        CourseScore studentCourse = courseScoreRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("未找到该学生课程记录"));

        // 2. 更新成绩
        studentCourse.setScore(score);

        // 3. 保存更新
        return courseScoreRepository.save(studentCourse);
    }


}
